package com.flyoil.petromp.view;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.cnpc.fyutils.EditText_util;
import com.flyoil.petromp.R;

/**
 * Created by songzhiiyn on 2017/10/16.
 * 自定义搜索框，搜索功能、清除数据功能、返回功能、设置提示语
 */

public class SearchDataView extends LinearLayout {
    private View view;
    private Activity mActivity;
    private LinearLayout line_back, line_delete, line_search;
    private EditText edt_message;
    private OnListener onListener;

    public SearchDataView(Context context) {
        super(context);
        initview(context);
    }

    public SearchDataView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initview(context);
    }

    public SearchDataView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initview(context);
    }

    private void initview(Context context) {
        view = LayoutInflater.from(context).inflate(R.layout.view_search_data_layout, this);
        line_back = view.findViewById(R.id.line_search_data_view_back);
        line_delete = view.findViewById(R.id.line_search_data_view_delete);
        line_search = view.findViewById(R.id.line_search_data_view_search);
        edt_message = view.findViewById(R.id.edt_search_data_view_message);
        line_back.setOnClickListener(onClickListener);
        line_search.setOnClickListener(onClickListener);
        line_delete.setOnClickListener(onClickListener);
        EditText_util.seteidttext_astrictcharacter(edt_message);
    }

    public void setOncListener(OnListener onListener, Activity mActivity) {
        this.onListener = onListener;
        this.mActivity = mActivity;
    }

    public void setTextHint(String hint) {
        edt_message.setHint(hint);
    }

    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.line_search_data_view_back:
                    if (onListener != null) {
                        onListener.Onback();
                    }
                    break;
                case R.id.line_search_data_view_delete:
                    edt_message.setText("");
                    break;
                case R.id.line_search_data_view_search:
                    if (onListener != null) {
                        EditText_util.hideKeyboard(mActivity);
                        onListener.Onsearch(edt_message.getText().toString());
                    }
                    break;

            }
        }
    };

    public interface OnListener {
        public void Onback();

        public void Onsearch(String message);
    }

}
